package com.fs.system.security;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.fs.common.constant.HttpStatus;
import com.fs.common.core.vo.AjaxResult;
import com.fs.common.util.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author suke
 * @version 1.0
 * @title TokenAuthenticationEntryPoint
 * @description 二次token认证失败的入口
 * @create 2024/7/26 14:39
 */
@Component
public class TokenAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        int code = HttpStatus.UNAUTHORIZED;
        String msg = StrUtil.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
        //response.getWriter().print(JSON.toJSONString(AjaxResult.error(code,msg)));
        ServletUtils.renderString(response,JSON.toJSONString(AjaxResult.error(code,msg)));
    }
}
